package com.erlinyou.im.service.group.Impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.erlinyou.base.service.BaseServiceImpl;
import com.erlinyou.im.dao.repository.chat.ChatMessageRepository;
import com.erlinyou.im.dao.repository.chat.ChatSessionRepository;
import com.erlinyou.im.dao.repository.group.GroupRepository;
import com.erlinyou.im.dao.repository.group.GroupmembersRepository;
import com.erlinyou.im.model.group.Group;
import com.erlinyou.im.model.group.Groupmembers;
import com.erlinyou.im.service.group.IGroupMemService;
@Service
public class GroupMemServiceImpl extends BaseServiceImpl<Groupmembers, Integer> implements IGroupMemService {
	@Resource
	private ChatSessionRepository chatSessionRepository;
	
	@Resource
	private ChatMessageRepository chatMessageRepository;
	
	@Resource
	private GroupRepository groupRepository;

	private GroupmembersRepository repository;

	@Resource
	public void setBaseService(GroupmembersRepository repository) {
		this.repository = repository;
		this.baseRepository = repository;
	}
	
	
	@Override
	@Cacheable(value="serviceCache",key="#root.methodName+':'+#rid")
	public List<Groupmembers> findByGroupid(int rid) {
		return repository.findByGroupid(rid);
	}
	
	@Override
	@Cacheable(value="serviceCache",key="#root.methodName+':'+#rid+':'+#userid")
	public Groupmembers findByGroupidAndUserid(int rid,Long userid) {
		return repository.findByGroupidAndUserid(rid,userid);
	}

	@Override
	@Transactional
	public boolean deleteByUseridByGroupid(Long userid, Integer groupid) throws RuntimeException {
		int num = repository.deleteByUseridAndGroupid(userid, groupid);
		if(num>0) {
			int num1 = chatSessionRepository.deleteByUseridAndRidAndCtype(userid,groupid.longValue(),2);
			if(num1>0) {
				return true;
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
	}
	@Override
	@Transactional
	public boolean deleteByUseridByGroupid(Long userid,Group group) throws RuntimeException {
		int num = repository.deleteByUseridAndGroupid(userid, group.getId());
		if(num>0) {
			chatSessionRepository.deleteByUseridAndRidAndCtype(userid,group.getId().longValue(),2);
			if(groupRepository.save(group)!=null) {
				return true;
			}else {
				throw new RuntimeException();
			}
		}else {
			throw new RuntimeException();
		}
	}

	@Override
	@Transactional
	public boolean deletegro(Integer groupid,Long userId) throws RuntimeException {
 		int num = repository.deleteByGroupid(groupid);
		if(num>0) {
			groupRepository.deleteById(groupid);
			chatSessionRepository.deleteByRidAndCtype(groupid.longValue(),2);
				int byGid = chatMessageRepository.deleteByGid(String.valueOf(groupid));
				if(byGid>0) {
					return true;
				}else {
					throw new RuntimeException();
				}
		}else {
			throw new RuntimeException();
		}
	}


	@Transactional
	@Override
	public boolean saveImgAndDel(List<Groupmembers> groupmembers, Group group) {
		repository.deleteAll(groupmembers);
		if(groupRepository.save(group) != null) {
			return true;
		}else {
			throw new RuntimeException();
		}
	}


	@Override
	public List<Map<String, Object>> searchByUserName(Long userId,String mohu) {
		return repository.searchByUserName(userId,mohu);
	}


	@Override
	public List<Map<String, Object>> searchByGroupName(Long userId, String mohu) {
		return repository.searchByGroupName(userId,mohu);
	}

}
